package com.leedsoft.controller;

import com.leedsoft.common.AjaxReply;
import com.leedsoft.common.JSON;
import com.leedsoft.common.Page;
import com.leedsoft.controller.dto.AidPlanDto;
import com.leedsoft.controller.dto.SaveStepDto;
import com.leedsoft.controller.dto.StepDto;
import com.leedsoft.controller.dto.StepInfoDto;
import com.leedsoft.controller.vo.AidPageVo;
import com.leedsoft.controller.vo.StepVo;
import com.leedsoft.controller.vo.TaskJsonVo;
import com.leedsoft.domain.AidPlan;
import com.leedsoft.service.AidService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/aid")
public class AidController {
    private static final Logger logger = LoggerFactory.getLogger(AidController.class);
    @Autowired
    private AidService aidService;

    //返回view
    @RequestMapping(value = {"/", ""})
    public ModelAndView get(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("aid_main");
        return modelAndView;
    }

    //返回view
    @RequestMapping(value = {"/test"})
    @ResponseBody
    public Map<String, Object> test(HttpServletRequest request) {
        aidService.testDao();
        return AjaxReply.success();
    }

    //分页实例
    @RequestMapping(value = {"/page"})
    public ModelAndView testPage(AidPageVo pageVo) {
        ModelAndView modelAndView = new ModelAndView("/test");
        Page<AidPlanDto> page = aidService.getAidPlanList(pageVo);
        return modelAndView;
    }

//--------------------------------------------------------------------------------

    // TODO: 2017/6/19 加载流程定义页面 接口4
    @RequestMapping(value = "procdef/update", method = RequestMethod.GET)
    public ModelAndView toProcdefUpdate(Long planId, Long stepId) {
        Assert.notNull(planId, "参数错误：planId不可为空");
        AidPlan plan = aidService.getAidPlan(planId);
        Assert.notNull(plan, "planId不存在");
        ModelAndView modelAndView = new ModelAndView("/proc_def");
        modelAndView.addObject("plan", plan);
        //step list
        List<StepDto> steps = aidService.getSteps(planId);
        modelAndView.addObject("steps", steps);
        //step info
        if (!(stepId != null && stepId == -1)) {//stepId=-1表示新建节点
            if (steps.size() > 0) {
                if (stepId == null) {
                    stepId = steps.get(steps.size() - 1).getId();
                }
                StepInfoDto stepInfo = aidService.getStepInfo(stepId);
                modelAndView.addObject("stepInfoJson", JSON.gson.toJson(stepInfo));
                modelAndView.addObject("stepInfo", stepInfo);
            }
        }
        return modelAndView;
    }

    // TODO: 2017/6/19 保存修改流程定义环节接口
    @RequestMapping("procdef/step/load")
    @ResponseBody
    public Map<String, Object> loadStep(Long stepId) {
        StepInfoDto stepInfo = aidService.getStepInfo(stepId);
        return AjaxReply.success(stepInfo);
    }

    // TODO: 2017/6/19 保存修改流程定义环节接口
    @RequestMapping("procdef/step/save")
    @ResponseBody
    public Map<String, Object> saveStep(StepVo stepVo) {
        SaveStepDto dto = aidService.saveStep(stepVo);
        return AjaxReply.success(dto);
    }

    // TODO: 2017/6/29 删除环节接口
    @RequestMapping("procdef/step/del")
    @ResponseBody
    public Map<String, Object> delStep(Long stepId) {
        aidService.delStep(stepId);
        return AjaxReply.success();
    }
    // TODO: 2017/6/29 删除环节接口
    @RequestMapping("procdef/task/load")
    @ResponseBody
    public Map<String, Object> delStep(Long planId, Integer taskCode) {
        TaskJsonVo taskVo = aidService.loadTask(planId, taskCode);
        return AjaxReply.success();
    }


    // 流程定义成功后，返回的完整的流程图 接口7 : /acti/getProcDefImg.do?procDefId=${procDefId}
}
